﻿using AngleSharp.Io;
using Furion;
using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;

namespace GWB.Core.Common
{

    public static class RedisHelper
    {
       public static readonly ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(App.Configuration["Redis:ProxyAddress"]);

       /// <summary>
       /// 读取
       /// </summary>
       /// <param name="key"></param>
       /// <param name="dbInt"></param>
       /// <returns></returns>
       public static string Read(string key,int dbInt=0)
        {
            if (dbInt==1)
            {
                key = "record:" + key;
            }
            IDatabase db = redis.GetDatabase(dbInt);
            string value = db.StringGet(key);
            return value;
        }


       /// <summary>
       /// 写入
       /// </summary>
       /// <param name="keyValue"></param>
       /// <param name="dbInt"></param>
       /// <returns></returns>
       public static bool Write(Dictionary<string, string> keyValue,int dbInt = 0)
        {
            IDatabase db = redis.GetDatabase(dbInt);
            foreach (KeyValuePair<string, string> kv in keyValue)
            {
                if (dbInt == 1)
                {
                    db.StringSetAsync("record:" + kv.Key, kv.Value);
                }
                else
                {
                    db.StringSetAsync(kv.Key, kv.Value);
                }
            }
            return true;
        }

       /// <summary>
       /// 写入
       /// </summary>
       /// <param name="keyValue"></param>
       /// <param name="dbInt"></param>
       /// <returns></returns>
       public static bool Write(string key,string value, int dbInt = 0)
       {
           IDatabase db = redis.GetDatabase(dbInt);
           if (dbInt == 1)
           {
               db.StringSetAsync("record:" + key, value);
           }
           else
           {
               db.StringSetAsync(key, value);
           }
           return true;
       }

       /// <summary>
       /// 写入
       /// </summary>
       /// <param name="keyValue"></param>
       /// <param name="dbInt"></param>
       /// <returns></returns>
       public static bool Write(string key, string value,TimeSpan? timeSpan, int dbInt = 0)
       {
           IDatabase db = redis.GetDatabase(dbInt);
           if (dbInt == 1)
           {
               db.StringSetAsync("record:" + key, value, timeSpan);
           }
           else
           {
               db.StringSetAsync(key, value);
           }
           return true;
       }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dbInt"></param>
        /// <returns></returns>
        public static bool Del(string key, int dbInt = 0)
       {
           if (dbInt == 1)
           {
               key = "record:" + key;
           }
           IDatabase db = redis.GetDatabase(dbInt);
           return db.KeyDelete(key);
       }
    }
}
